package com.cskaoyan.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class MybatisUtils {

    static SqlSession sqlSession;

    static {

        // 1. 获取sqlSessionFactoryBuilder
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();

        // 2. 获取sqlSessionFactory
//        InputStream inputStream = Main.class.getClassLoader().getResourceAsStream("mybatis-config.xml");
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream("mybatis-config.xml");
        } catch (IOException e) {
            e.printStackTrace();
        }

        SqlSessionFactory sqlSessionFactory = builder.build(inputStream);


        // 3. 获取sqlSession
        sqlSession = sqlSessionFactory.openSession();

    }



    // 获取sqlSession的方法
    public static SqlSession getSqlSession(){
        return sqlSession;
    }
}
